package com.request;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;

import java.util.Iterator;

import com.design.Layers;

public class RouterRequest extends Thread
{
	private Details details=null;
	private String [] routerInfo=new String[1];
	private String [] serverInfo=new String[1];
	private int port=0;
	private String system=null;
	private ServerSocket serverSocket=null;
	private Socket socket=null;
	private ObjectInputStream objectInputStream=null;
	private Vector vectorSize=null;
	private SocketConnection socketConnection=null;
	private String serverPort=null;
	private String serverSystem=null;
	private ObjectOutputStream objectOutputStream=null;
	private TreeMap treeMap=new TreeMap();
	private TreeSet transSet=new TreeSet();
	private ArrayList list=new ArrayList();
	ClientRequestVo clientRequestVo=new ClientRequestVo();
	private String[] clientInfo=new String[2];
	int thresold;
	public int value=0;
public RouterRequest() 
{
	
	details=new Details();
	thresold=details.Thresold();
	routerInfo=details.Config("routerport","routersystem");
	System.out.println(routerInfo[0]);
	System.out.println(routerInfo[1]);
	port=Integer.parseInt(routerInfo[0]);
	system=routerInfo[1];
	serverInfo=details.Config("serverport","serversystem");
	serverPort=serverInfo[0];
	serverSystem=serverInfo[1];
	clientInfo=details.Config("clientport","clientsystem");
//	System.out.println("server"+serverPort);
	
	vectorSize=new Vector();
	
	start();
}
public void run()
{
	try
	{
		serverSocket=new ServerSocket(port) ;
		while(true)
		{
			
			System.out.println("recv......");
			socket=serverSocket.accept();
			objectInputStream=new ObjectInputStream(socket.getInputStream());
			Object object=objectInputStream.readObject();
			if(object instanceof ClientRequestVo)
			vectorSize.add(object);
			if(vectorSize.size()==thresold)
			{
				for(int vec=0;vec<thresold;vec++)
				{++value;
					Vector table=new Vector();
					clientRequestVo=(ClientRequestVo)vectorSize.get(vec);
					System.out.println("@@@@@@@@@@@@@@@@@@");
					System.out.println(clientRequestVo.getRequest());
					System.out.println(clientRequestVo.getTransmissionRate());
					System.out.println("@@@@@@@@@@@@@@@@@@");
					treeMap.put(clientRequestVo.getTransmissionRate(), clientRequestVo);
					transSet.add(clientRequestVo.getTransmissionRate() );
					table.add(value);
					table.add(clientRequestVo.getSystem());
					table.add(clientRequestVo.getPort());
					table.add(clientRequestVo.getTransmissionRate());
					table.add(serverSystem);
					table.add(serverPort);
					String[] request=clientRequestVo.getRequest().split(" ");
					table.add(request[0]);
					table.add("Waiting...");
					Layers.routerTab.addRow(table);
				}
				System.out.println(treeMap);
				Iterator iterator=transSet.iterator();
				while(iterator.hasNext())
				{
					list.add(iterator.next());
				}
				for(int treeValue=treeMap.size()-1;treeValue>=0;treeValue--)
				{
					int transRate=Integer.parseInt(list.get(treeValue).toString());
					Object obj=treeMap.get(transRate);
					ClientRequestVo clientRequestVo=(ClientRequestVo)obj;
					Socket socket=new Socket(serverSystem,Integer.parseInt(serverPort));
					ObjectOutputStream objectOutputStream=new ObjectOutputStream(socket.getOutputStream());
					objectOutputStream.writeObject(clientRequestVo);
				}
				vectorSize.clear();
				treeMap.clear();
				transSet.clear();
				list.clear();
				Thread.sleep(3000);
			}
			else if(object instanceof ServerResponseVO)
			{
				ServerResponseVO serverResponseVO=(ServerResponseVO)object;
				//System.out.println("Router"+serverResponseVO.getPort());
				//System.out.println("Router"+serverResponseVO.getSystem());
				for(int i=0;i<Layers.routerTab.getRowCount();i++)
				{
					System.out.println("row count...."+Layers.routerTab.getValueAt(i, 6).toString());
					String[] request=Layers.routerTab.getValueAt(i, 6).toString().split(" ");
					if(request[0].equalsIgnoreCase(serverResponseVO.getRequest()))
							{
						Layers.routerTab.setValueAt("Responsed", i,7);
							}
				}
				int port=Integer.parseInt(clientInfo[0]);
				SocketConnection socketConnection=new SocketConnection();
				objectOutputStream=socketConnection.SocketSend(port, clientInfo[1]);
				objectOutputStream.writeObject(serverResponseVO);
			}
			}
	}
	catch (Exception e) 
	{
	e.printStackTrace();	
	}
}
public static void main(String[] args) 
{
RouterRequest routerRequest=new RouterRequest();

}
}
